package PFE;

import java.io.*;
import java.net.InetAddress;
import java.util.StringTokenizer;

import org.jdom.*;
import org.jdom.output.*;

import snmp.SNMPObject;
import snmp.SNMPObjectIdentifier;
import snmp.SNMPSequence;
import snmp.SNMPVarBindList;
import snmp.SNMPv1CommunicationInterface;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class XmlReseaux 
{
	int nb;
   static Element racine = new Element("Reseaux");
   static org.jdom.Document document = new Document(racine);

   public XmlReseaux(int NbNoeuds, String ip[],String NomMachine[])
   {
	   int id=1;
	   racine.removeContent();
	   enregistre("Reseaux.xml"); 
	   for (int i=0;i<NbNoeuds;i++)
	   {
		   Element eqpt = new Element("Equipement");
		   racine.addContent(eqpt);		  	
		   Attribute ID = new Attribute("ID",""+id);
		   eqpt.setAttribute(ID);
		   id++;
		   Element Ip = new Element("IP");
		   Ip.setText(ip[i]);
		   eqpt.addContent(Ip);
		  
		   Element nom = new Element("Nom");
		   nom.setText(NomMachine[i]);
		   eqpt.addContent(nom);
		  
		   Element cat = new Element("Categorie");
		   String oid=getOID(ip[i],"public","1.3.6.1.2.1.1")[0]; 
		   String testcat= valeurOID(ip[i],oid,"public"); 
		   if (testcat.indexOf("Windows")!=-1) cat.setText("pc");
		   else if (testcat.indexOf("Linux")!=-1||testcat.indexOf("Cisco")!=-1) cat.setText("routeur");
		   else if (testcat.indexOf("RICOH")!=-1) cat.setText("printer");
		   else cat.setText("inconnu");
		   eqpt.addContent(cat);
		  
		   affiche();
		   enregistre("Reseaux.xml");
		  }	   
   	}
   static void affiche()
   {
      try
      {
         XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
         sortie.output(document, System.out);
      }
      catch (java.io.IOException e){}
   }

   static void enregistre(String fichier)
   {
	   File f=new File(fichier);
	   f.delete();
      try
      {
         XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
         sortie.output(document, new FileOutputStream(fichier));
      }
      catch (java.io.IOException e){}
   }



   public String valeurOID (String Ip,String OID,String community){
	   String res=null;
	   try
	   {
		
		   int version = 0;    // SNMPv1
		   InetAddress hostAddress = InetAddress.getByName(Ip);
		   SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
        
		   StringTokenizer st = new StringTokenizer(OID, " ,;");
        
		   while (st.hasMoreTokens())
		   {
			   String itemID = st.nextToken();    
			   SNMPVarBindList newVars = comInterface.getMIBEntry(itemID);
			   SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
			   SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
			   SNMPObject snmpValue = pair.getSNMPObjectAt(1);
			   String typeString = snmpValue.getClass().getName();
            
			   if (typeString.equals("snmp.SNMPOctetString"))
			   {
				   String snmpString = snmpValue.toString();
				   
				   // truncate at first null character
				   int nullLocation = snmpString.indexOf('\0');
				   if (nullLocation >= 0)
					   snmpString = snmpString.substring(0,nullLocation);
                
				   res=snmpString;                    
			   }
			   else
			   {
				   res=snmpValue.toString();
			   }
		   }    
	   }
	   catch(InterruptedIOException e)
	   {
		   res=null;
		   System.out.println("Interrupted during retrieval:  " + e + "\n");
	   }
	   catch(Exception e)
	   {
		   res=null;
		   System.out.println("Exception during retrieval:  " + e + "\n");
	   }
	   return res;
   }
   
   public String [] getOID (String Ip,String community,String oidR)
	{
		String [] oid;
		oid= new String [5000];
    	for (int i=0;i<5000;i++)oid[i]="";
		try
       {
	    nb=0;
		InetAddress hostAddress = InetAddress.getByName(Ip);
       SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(0, hostAddress, community);
   
       String itemID = "";            
       String retrievedID = oidR;      
       String c1= oidR;  
       String c2=c1;    
       
    	
    	int i=0;
       while (!retrievedID.equals(itemID)&& c2.equals(c1))
           
       {
      
           itemID = retrievedID;
           
           SNMPVarBindList newVars = comInterface.getNextMIBEntry(itemID);
           
           SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
           SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
           SNMPObject snmpValue = pair.getSNMPObjectAt(1);
           retrievedID = snmpOID.toString();
           String oid0=snmpOID.toString();
 
           String typeString = snmpValue.getClass().getName();
           c2=retrievedID.substring(0,c1.length());
           //System.out.println("C1 :"+c1+"  C2 : "+c2);
           if (typeString.equals("snmp.SNMPOctetString"))
           {
               String snmpString = snmpValue.toString();
               
               int nullLocation = snmpString.indexOf('\0');
               if (nullLocation >= 0)
                   snmpString = snmpString.substring(0,nullLocation);
               
               oid[i]= snmpOID.toString();                
               //System.out.println("OID en cours : "+oid[i]);
               i++;
               nb++;
           }
           else
           {
           	oid[i]= snmpOID.toString();
           	//System.out.println("OID en cours : "+oid[i]);
           	i++;
           	nb++;
           }

       }
       return oid;
       }
       catch(InterruptedIOException e)
       {
       	System.out.println("Interrupt"+e);
       	return oid;
       }
       catch(Exception e)
       {
       	System.out.println("Exception "+e);
       	return oid;
       }
       catch(Error err)
       {
       	System.out.println("Erreur"+err);
       	return oid;
       }
	}


}

